package rtda

// Java虚拟机栈，由栈帧组成
// 栈帧中保存方法执行的状态
type Stack struct {
	maxSize uint	// 栈的容量
	size uint	// 栈的当前大小
	_top *Frame	// 栈顶指针
}

func newStack(maxSize uint) *Stack {
	return &Stack{
		maxSize:maxSize,
	}
}

func (self *Stack)push(frame *Frame) {
	if self.size >= self.maxSize {
		panic("java.lang.StackOverflowError")
	}

	if self._top != nil {
		frame.lower = self._top
	}

	self._top = frame
	self.size++
}

func (self *Stack)pop() *Frame {
	if self._top == nil {
		panic("java stack is empty!")
	}

	top := self._top
	self._top = top.lower
	top.lower = nil
	self.size--
	return top
}

func (self *Stack)top() *Frame {
	if self._top == nil {
		panic("jvm stack is empty")
	}
	return self._top
}

func (self *Stack)isEmpty() bool {
	return self._top == nil
}